package com.ss.android.article.base.ui;

import android.content.Context;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.feed.c.FeedDownloadAppAd;
import com.ss.android.article.base.feature.model.CellRef;
import com.ss.android.article.browser.R;
import com.ss.android.b.a.AdsAppItem;
import com.ss.android.b.a.BaseAdsData;
import com.ss.android.common.ad.MobAdClickCombiner;
import com.ss.android.common.download.DownloadAdapterProxy;
import com.ss.android.common.download.DownloadShortInfo;
import com.ss.android.common.download.DownloadInfoChangeListener;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.common.util.ToolUtils;
import com.ss.android.newmedia.util.AppUtil;

import java.lang.ref.WeakReference;

import org.json.JSONObject;

public class AdButtonLayout extends RelativeLayout {
    final class a implements DownloadInfoChangeListener {
        public final AdButtonLayout b;
        long a;

        a(AdButtonLayout arg1) {
            this.b = arg1;

        }

        public final void setDownloadId(long id) {
            this.a = id;
        }

        public final void onDownloadProgressChanged(DownloadShortInfo arg10, int arg11, long arg12, long arg14, long arg16) {
            this.b.b.post(new b(this, arg10, arg11, arg12, arg14));
        }
    }

    public final class AdButtonLayout$b extends AsyncTask {
        private final AdButtonLayout a;

        public AdButtonLayout$b(AdButtonLayout arg1) {
            this.a = arg1;

        }

        protected final Object doInBackground(Object[] arg7) {
            Object v0_1 = null;
            if (arg7 != null) {
                if (arg7.length > 0 && (TextUtils.isEmpty((CharSequence) arg7[0]))) {
                    return null;
                }

                if (this.a.a == null) {
                    return null;
                }

                Object v1 = arg7[0];
                DownloadShortInfo v0 = DownloadAdapterProxy.inst().queryDownloadInfo(this.a.a, ((String) v1));
                if (v0 == null) {
                    return null;
                }

                Logger.d("AdButtonLayout queryDownloadInfo", "result.id = " + v0.id + " url = " + (((String) v1)) + " fileName = " + v0.fileName);
            }

            return v0_1;
        }

        protected final void onPostExecute(Object arg11) {
            Resources v1;
            TextView v2;
            String v0_3;
            CharSequence v7 = null;
            int v0 = R.string.ke;
            super.onPostExecute(arg11);
            if (!this.isCancelled()) {
                this.a.c();
                boolean v3 = AppUtil.a(this.a.a, this.a.e.version, this.a.e.pkg);
                try {
                    if ((AppData.getInstance().allowInsideDownloadManager()) && arg11 != null && ((DownloadShortInfo) arg11).id > -1 && !DownloadAdapterProxy.inst().isDownloadSuccessAndFileNotExist(this.a.a, ((DownloadShortInfo) arg11)) && !v3) {
                        Logger.d("AdButtonLayout QueryDownloadInfoTask", "result.id = " + ((DownloadShortInfo) arg11).id + " fileName = " + ((DownloadShortInfo) arg11).fileName);
                        int v5 = 7;
                        if (!this.a.a()) {
                            v5 = 6;
                        }

                        DownloadAdapterProxy.inst().registerDownloadListener(this.a.a, Long.valueOf(((DownloadShortInfo) arg11).id), this.a.g, String.valueOf(this.a.e.id), v5, this.a.e.logExtra);
                        this.a.f = ((DownloadShortInfo) arg11);
                        this.a.a(false);
                        switch (((DownloadShortInfo) arg11).b) {
                            case 1:
                            case 2: {
                                this.a.a(true);
                                v0_3 = this.a.getResources().getString(com.ss.android.article.browser.R.string.kf);
                                break;
                            }
                            case 4: {
                                this.a.a(true);
                                v0_3 = this.a.getResources().getString(com.ss.android.article.browser.R.string.ki);
                                break;
                            }
                            case 8: {
                                this.a.a(true);
                                if (ToolUtils.d(this.a.a, ((DownloadShortInfo) arg11).fileName)) {
                                    v0_3 = this.a.getResources().getString(com.ss.android.article.browser.R.string.ke);
                                    break;
                                }

                                v0_3 = this.a.getResources().getString(com.ss.android.article.browser.R.string.k9);
                                break;

                            }
                            case 16: {
                                v0_3 = this.a.getResources().getString(com.ss.android.article.browser.R.string.kh);
                                break;
                            }
                            default: {
                                v0_3 = null;
                                break;
                            }
                        }
                    } else {
                        label_136:
                        if (this.a.f != null) {
                            DownloadAdapterProxy.inst().unregisterDownloadListener(this.a.a, Long.valueOf(this.a.f.id), this.a.g);
                        }

                        this.a.f = null;
                        if (ToolUtils.isAppInstalled(this.a.a, this.a.e.pkg)) {
                            v2 = this.a.d;
                            v1 = this.a.getResources();
                        } else {
                            v2 = this.a.d;
                            v1 = this.a.getResources();
                            v0 = v3 ? R.string.kj : R.string.ka;
                        }

                        UIUtils.setText(v2, v1.getString(v0));
                        this.a.a(false);
                        return;
                    }
                    label_69:
                    UIUtils.setText(this.a.d, ((CharSequence) v0_3));
                    if (((DownloadShortInfo) arg11).c > 0 && (UIUtils.isVisible(this.a.c))) {
                        this.a.c.setProgress(((int) (((DownloadShortInfo) arg11).d * 100 / ((DownloadShortInfo) arg11).c)));
                        return;
                    }

                    this.a.c.setProgress(0);
                } catch (Exception v0_1) {
                    label_134:
                    v0_1.printStackTrace();
                }
            }
        }
    }

    protected Context a;
    protected RelativeLayout b;
    protected ProgressBar c;
    protected TextView d;
    protected FeedDownloadAppAd e;
    DownloadShortInfo f;
    final a g;
    int h;
    private boolean i;
    private boolean j;
    private WeakReference k;
    private AdButtonLayout$b l;
    private boolean m;
    private boolean n;
    private View.OnClickListener o;

    public AdButtonLayout(Context arg3) {
        super(arg3);
        this.i = false;
        this.j = false;
        this.b = null;
        this.c = null;
        this.d = null;
        this.g = new a(this);
        this.m = false;
        this.n = false;
        this.h = 0;
        this.o = new com.ss.android.article.base.ui.a(this);
        this.a(arg3);
    }

    public AdButtonLayout(Context arg3, AttributeSet arg4) {
        super(arg3, arg4);
        this.i = false;
        this.j = false;
        this.b = null;
        this.c = null;
        this.d = null;
        this.g = new a(this);
        this.m = false;
        this.n = false;
        this.h = 0;
        this.o = new com.ss.android.article.base.ui.a(this);
        this.a(arg3);
    }

    public AdButtonLayout(Context arg3, AttributeSet arg4, int arg5) {
        super(arg3, arg4, arg5);
        this.i = false;
        this.j = false;
        this.b = null;
        this.c = null;
        this.d = null;
        this.g = new a(this);
        this.m = false;
        this.n = false;
        this.h = 0;
        this.o = new com.ss.android.article.base.ui.a(this);
        this.a(arg3);
    }

    public final boolean a(CellRef arg7, BaseAdsData arg8, boolean arg9) {
        boolean v0 = false;
        if (arg8 == null || !(arg8 instanceof FeedDownloadAppAd)) {
            this.d();
        } else if (arg8.id <= 0) {
            this.d();
        } else {
            if (arg7 != null) {
                this.m = arg7.reuseItem;
            }

            this.e = ((FeedDownloadAppAd) arg8);
            if ((this.a()) && !arg9 && this.e.uiType == 1) {
                this.d();
                return v0;
            }

            if ((this.a()) && (arg9) && this.e.uiType != 1) {
                this.d();
                return v0;
            }

            if (android.support.design.a.isTextEmpty(this.e.buttonText)) {
                if ("app".equals(this.e.type)) {
                    this.e.buttonText = this.a.getResources().getString(com.ss.android.article.browser.R.string.j2);
                } else if ("action".equals(this.e.type)) {
                    this.e.buttonText = this.a.getResources().getString(com.ss.android.article.browser.R.string.call_now);
                } else if ("web".equals(this.e.type)) {
                    this.e.buttonText = this.a.getResources().getString(com.ss.android.article.browser.R.string.ad_label_detail);
                }
            }

            if (this.e.uiType == 1) {
                this.d.setTextColor(ContextCompat.getColor(this.a, R.color.ssxinzi5));
            }

            if ((this.a()) && this.e.uiType != 1) {
                this.setPadding(this.getPaddingLeft(), this.getPaddingTop(), ((int) UIUtils.sp2px(this.a, 6f)), this.getPaddingBottom());
            }

            if ("app".equals(this.e.type)) {
                this.e();
            } else {
                this.c();
            }

            this.b();
            v0 = true;
        }

        return v0;
    }

    public final void a(CellRef arg4) {
        if (arg4 == null || arg4.article == null) {
            this.d();
        } else {
            this.m = arg4.reuseItem;
            this.a(null, arg4.article.aF, false);
        }
    }

    private void a(Context arg3) {
        this.a = arg3;
        if (this.b == null) {
            this.b = (RelativeLayout) ((LayoutInflater) arg3.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(this.getInflateLayoutId(), ((ViewGroup) this));
            this.b.setOnClickListener(this.o);
            this.c = this.b.findViewById(com.ss.android.article.browser.R.id.btn_ad_progress_bar);
            this.d = this.b.findViewById(com.ss.android.article.browser.R.id.btn_ad_tv);
        }
    }

    public final void a(Boolean arg3) {
        if (arg3.booleanValue()) {
            UIUtils.setVisibility(this.d, 0);
        } else {
            UIUtils.setVisibility(this.d, 8);
        }
    }

    protected void a(boolean arg3) {
        if (this.j != arg3) {
            this.j = arg3;
            ProgressBar v1 = this.c;
            int v0 = arg3 ? 0 : 8;
            UIUtils.setVisibility(((View) v1), v0);
            if (arg3) {
                this.d.setBackgroundResource(com.ss.android.article.browser.R.drawable.e);
                return;
            }

            this.d.setBackgroundResource(com.ss.android.article.browser.R.drawable.jy);
        }
    }

    protected boolean a() {
        return true;
    }

    public final boolean a(BaseAdsData arg3) {
        return this.a(null, arg3, false);
    }

    public void b(boolean arg4) {
        if (this.i != arg4) {
            this.i = arg4;
            if (this.d != null) {
                this.d.setTextColor(ContextCompat.getColor(this.a, R.color.ssxinzi8));
                if (this.j) {
                    this.d.setBackgroundResource(com.ss.android.article.browser.R.drawable.e);
                } else {
                    this.d.setBackgroundResource(com.ss.android.article.browser.R.drawable.jy);
                }
            }

            if (this.c == null) {
                return;
            }
            this.c.setProgressDrawable(this.getResources().getDrawable(com.ss.android.article.browser.R.drawable.video_ad_button_progress_bg));
            this.c.getProgressDrawable().setBounds(this.c.getProgressDrawable().getBounds());
        }
    }

    protected void b() {
        JSONObject v8 = null;
        long v6 = 0;
        int v9 = 2;
        if ((this.a()) && !this.n && !this.m) {
            JSONObject v0 = null;
            try {
                v8 = new JSONObject();
                if (android.support.design.a.isTextEmpty(this.e.logExtra)) {
//                    goto label_20;
                } else {
                    v8.put("log_extra", this.e.logExtra);
                }
            } catch (Exception v0_1) {
            }

            label_20:
            if ("app".equals(this.e.type)) {
                MobAdClickCombiner.onAdEvent(this.a, this.getEventName(), "card_show", this.e.id, v6, v8, v9);
                return;
            }

            if ("action".equals(this.e.type)) {
                MobAdClickCombiner.onAdEvent(this.a, this.getEventName(), "card_show", this.e.id, v6, v8, v9);
            }
        }
    }

    final void c() {
        UIUtils.setText(this.d, this.e.buttonText);
        UIUtils.setVisibility(this.b, 0);
        AppData.getInstance();
        AppData.isNightMode();
        if (!this.a() || this.e.uiType == 1) {
            this.b.setBackgroundResource(com.ss.android.article.browser.R.drawable.ad_action_btn_begin_bg);
        }

        UIUtils.setVisibility(this.d, 0);
    }

    public final void d() {
        UIUtils.setVisibility(this.b, 4);
        UIUtils.setVisibility(this.d, 8);
        UIUtils.setVisibility(this.c, 8);
        if (AppData.getInstance().allowInsideDownloadManager()) {
            this.g();
        }

        this.setPadding(this.getPaddingLeft(), this.getPaddingTop(), 0, this.getPaddingBottom());
    }

    public final void e() {
        if (AppData.getInstance().allowInsideDownloadManager()) {
            this.g();
            this.l = new AdButtonLayout$b(this);
            com.bytedance.common.utility.b.a.a(this.l, new String[]{this.e.downloadUrl});
        }
    }

    final void f() {
        long v8 = 2;
        DownloadShortInfo v3 = null;
        int v2 = 2;
        if (this.e != null) {
            if ("app".equals(this.e.type)) {
                if (!NetworkUtils.isNetworkAvailable(this.a)) {
                    UIUtils.a(this.a);
                    return;
                }

                int v5 = 7;
                if (!this.a()) {
                    v5 = 6;
                }

                this.e.J = System.currentTimeMillis();
                if (AppData.getInstance().allowInsideDownloadManager()) {
                    this.e.a(this.h(), v2, this.f, this.g, v5);
                    return;
                }

                this.e.a(this.h(), v2, v3, null, v5);
                return;
            }

            if ("action".equals(this.e.type)) {
                JSONObject v10 = new JSONObject();
                try {
                    if (android.support.design.a.isTextEmpty(this.e.logExtra)) {
//                        goto label_53;
                    }else {
                        v10.put("log_extra", this.e.logExtra);
                    }
                } catch (Exception v0) {
                }

                label_53:
                if (this.a()) {
                    MobAdClickCombiner.onAdEvent(this.a, this.getEventName(), "click", this.e.id, v8, v10, v2);
                }

                if (this.e.actionType != 1) {
                    return;
                }

                if (android.support.design.a.isTextEmpty(this.e.phoneNumber)) {
                    return;
                }

                MobAdClickCombiner.onAdEvent(this.a, this.getEventName(), "click", this.e.id, v8, v10, v2);
                MobAdClickCombiner.onAdEvent(this.a, this.getEventName(), "click_call", this.e.id, ((long) this.e.actionType), v10, v2);
                ToolUtils.e(this.h(), this.e.phoneNumber);
                return;
            }

            if ("web".equals(this.e.type)) {
                try {
                    JSONObject v12 = new JSONObject();
                    if (!android.support.design.a.isTextEmpty(this.e.logExtra)) {
                        v12.put("log_extra", this.e.logExtra);
                    }

                    if (this.a()) {
                        MobAdClickCombiner.onAdEvent(this.a, this.getEventName(), "click", this.e.id, 0, v12, 2);
                        MobAdClickCombiner.onAdEvent(this.a, this.getEventName(), "ad_click", this.e.id, 0, v12, 2);
//                        goto label_125;
                    }else {
                        MobAdClickCombiner.onAdEvent(this.a, this.getEventName(), "click_landingpage", this.e.id, 0, v12, 1);
                    }
                } catch (Exception v0) {
                }

                label_125:
                AdsAppItem.a(this.h(), this.e.openUrl, this.e.webUrl, this.e.webTitle, this.e.orientation, new AdsAppItem.b(this.a, "embeded_ad", null, this.e.id, this.e.logExtra));
            }
        }
    }

    private void g() {
        if (this.f != null) {
            DownloadAdapterProxy.inst().unregisterDownloadListener(this.a, Long.valueOf(this.f.id), this.g);
        }

        if (this.l != null && this.l.getStatus() != AsyncTask.Status.FINISHED) {
            this.l.cancel(true);
        }
    }

    protected String getEventName() {
        String v0;
        if ("app".equals(this.e.type)) {
            v0 = "feed_download_ad";
        } else if ("action".equals(this.e.type)) {
            v0 = "feed_call";
        } else if ("web".equals(this.e.type)) {
            v0 = "embeded_ad";
        } else {
            v0 = "";
        }

        return v0;
    }

    protected int getInflateLayoutId() {
        return R.layout.bf;
    }

    private Context h() {
        Object v0;
        if (this.k == null || this.k.get() == null) {
            v0 = this.a;
        } else {
            v0 = this.k.get();
        }

        return ((Context) v0);
    }

    public void setContextRef(WeakReference arg2) {
        this.n = true;
        if (arg2 != null && arg2.get() != null) {
            this.k = arg2;
        }
    }
}

